//create by lwy by 2017年10月26日11:45:17

var page = 0;
var couponNum = 0;
var cardsStateValue = 0; //分类
$(function () {
    doNum();
    doCoupon();
})

//卡券状态点击
mui(".coupon_sort ul").on('tap','li',function(){
    mui('#refreshContainer').pullRefresh().scrollTo(0, 0);//滚动到顶部
    mui('#refreshContainer').pullRefresh().refresh(true);
    if($('.mui-pull-caption').html()=='上拉显示更多'){
        $('.mui-pull-caption').html('');
    }
    $('.coupon_box').empty();
    $(this).find('span').addClass('use_act');
    $(this).siblings().find('span').removeClass('use_act');
    cardsStateValue = $(this).attr('couponState');
    page=0;
    doCoupon();
})

function doNum() {
    $.ajax({
        url:'/userCardsC/searchUserCardsNum.json?',
        type:'json',
        data:{
            token:getItem('token'),
            companyCode:getItem('companyCode'),
        },
        type:'post',
        success:function (data) {
            if(data.code==1){
                $('#nouse').html('未使用('+data.datas.unused+')');
                $('#hasuse').html('已使用('+data.datas.used+')')
                $('#overdue').html('已过期('+data.datas.due+')')
            }
        }
    })
}


function doCoupon() {
    page++;
    $.ajax({
        url: '/userCardsC/searchCards.json',
        dataType: 'json',
        data: {
            token: getItem('token'),
            page: page,
            cardsStateValue: cardsStateValue,
            companyCode:getItem('companyCode'),
        },
        type: 'post',
        beforeSend: function () {
            if (page == 1) {
                $('<div class="loading"><img src="../phoneImg/jiazaizhong.gif"/></div>').appendTo($('.coupon_box'));
            }
        },
        success: function (data) {
            if (data.code == 1) {
                if (page == 1) {
                    $('.coupon_box').empty();
                    if (data.datas.data == 0) {
                        mui('#refreshContainer').pullRefresh().disablePullupToRefresh();
                        $('<div class="kong"><img src="../phoneImg/kong.png" alt="暂无数据"></div>').appendTo($('.coupon_box'));
                    }
                }
                var couponData = data.datas.data;

                for (var i = 0; i < couponData.length; i++) {
                    var ifhasUsed = '';
                    var couponOverdue = 'couponOverdue';
                    var cardHref = '';
                    var style = '';
                    if(couponData[i].cardsTypeValue==3){
                        cardHref = 'goods.html?goodsId='+couponData[i].goodsId;
                    }else if(couponData[i].cardsTypeValue==4){
                        cardHref = 'goods-list.html?goodsCategory_id='+couponData[i].goodsCategoryId;
                    }
                    if(cardsStateValue==1){
                        ifhasUsed = '<div class="overdue"><img src="../img/overdue.png" alt="已使用"></div>'
                    }else if(cardsStateValue==2){
                        //ifhasUsed = '<div class="overdue"><img src="../phoneImg/overdue2.png" alt="已过期"></div>'
                        style = 'background: url(../phoneImg/mycoupon_bg2.jpg) no-repeat 0 0;background-size: cover;';
                    }else{
                        couponOverdue = '';
                        ifhasUsed = '<div class="use" href="'+cardHref+'">立即使用</div>';
                    }
                    $('<div class="coupon mui-clearfix '+couponOverdue+'">' +
                        '<div class="coupon_left" style="'+style+'">' +
                        '<div class="decrease_money"><b>￥</b>'+couponData[i].money+'</div>' +
                        '</div><div class="coupon_right">' +
                        '<div class="coupon_name">'+couponData[i].name+'</div>' +
                        '<div class="coupon_time">'+couponData[i].beginTime+'-'+couponData[i].endUseTime+'</div>' +
                        '</div>'+ifhasUsed+'</div>').appendTo($('.coupon_box'));
                }
                mui('body').on('tap','.use',function () {
                    window.location.href=$(this).attr('href');
                })
                couponNum = couponData.length;
            }
        }
    })
    return couponNum;
}

//上拉加载
mui.init({
    pullRefresh: {
        container: "#refreshContainer",//待刷新区域标识，querySelector能定位的css选择器均可，比如：id、.class等
        up: {
            contentrefresh: "正在加载...",//可选，正在加载状态时，上拉加载控件上显示的标题内容
            contentnomore: '我是有底线的',//可选，请求完毕若没有更多数据时显示的提醒内容；
            callback: function () {
                setTimeout(function () {
                    var numResult = doCoupon();
                    if (numResult < 10) {
                        mui('#refreshContainer').pullRefresh().endPullupToRefresh(true);
                    } else {
                        mui('#refreshContainer').pullRefresh().endPullupToRefresh(false);
                    }
                }, 1000);
            } //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
        }
    }
});